package com.mediastory.spsserver.urgencyNotice.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.mediastory.spsserver.category.service.CategorySvc;
import com.mediastory.spsserver.urgencyNotice.domain.UrgencyNotice;
import com.mediastory.spsserver.urgencyNotice.service.UrgencyNoticeSvc;
import com.mediastory.spsserver.urgencyNoticeFile.service.UrgencyNoticeFileSvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping(value={"/urgencyNotice"})
public class UrgencyNoticeCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private  UrgencyNoticeSvc urgencyNoticeSvc;
	
	@Autowired
	UrgencyNoticeFileSvc urgencyNoticeFileSvc;
	
	@Autowired
	CategorySvc categorySvc;
	
	/**
	 * 목록 보기
	 * */
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		String name = ServletRequestUtils.getStringParameter(request, "name");
		String startDate = ServletRequestUtils.getStringParameter(request, "startDate");
		String endDate = ServletRequestUtils.getStringParameter(request, "endDate");
		String categoryIdx = ServletRequestUtils.getStringParameter(request, "categoryIdx");
		
		condition.put("name", name);
		condition.put("startDate", startDate);
		condition.put("endDate", endDate);
		condition.put("categoryIdx", categoryIdx);

		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(urgencyNoticeSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		//카테고리 목록
		resultMap.put("categoryList", categorySvc.findbyList(null));
		
		return resultMap;
	}

	/**
	 * 상세 보기
	 * */
	@RequestMapping(value={"/detail.do"})
	public Map<String, Object> detail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="idx")  Integer idx) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("idx", idx);

		resultMap.put("urgencyNotice", urgencyNoticeSvc.findby(condition));

		//파일 조회
		condition.put("urgencyNoticeIdx", idx);
		
		//1536X2048
		condition.put("fileType", "urgencyNoticeImg");
		resultMap.put("urgencyNoticeImg", urgencyNoticeFileSvc.findbyList(condition));
		
		//800X1280
		condition.put("fileType", "urgencyNoticeImg2");
		resultMap.put("urgencyNoticeImg2", urgencyNoticeFileSvc.findbyList(condition));
		
		//1080X1960
		condition.put("fileType", "urgencyNoticeImg3");
		resultMap.put("urgencyNoticeImg3", urgencyNoticeFileSvc.findbyList(condition));
		
		//640X960
		condition.put("fileType", "urgencyNoticeImg4");
		resultMap.put("urgencyNoticeImg4", urgencyNoticeFileSvc.findbyList(condition));	
		
		return resultMap;
	}
	
	/**
	 * 등록,수정 폼
	 * */
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, Object> condition = new HashMap<String, Object>();
		
		int idx = ServletRequestUtils.getIntParameter(request, "idx", 0);
		if(idx != 0){
			condition.put("idx", idx);
			resultMap.put("urgencyNotice", urgencyNoticeSvc.findby(condition));
			
			//파일 조회
			condition.put("urgencyNoticeIdx", idx);
			
			//1536X2048
			condition.put("fileType", "urgencyNoticeImg");
			resultMap.put("urgencyNoticeImg", urgencyNoticeFileSvc.findbyList(condition));
			
			//800X1280
			condition.put("fileType", "urgencyNoticeImg2");
			resultMap.put("urgencyNoticeImg2", urgencyNoticeFileSvc.findbyList(condition));
			
			//1080X1960
			condition.put("fileType", "urgencyNoticeImg3");
			resultMap.put("urgencyNoticeImg3", urgencyNoticeFileSvc.findbyList(condition));
			
			//640X960
			condition.put("fileType", "urgencyNoticeImg4");
			resultMap.put("urgencyNoticeImg4", urgencyNoticeFileSvc.findbyList(condition));	
		}
		
		//카테고리 목록
		resultMap.put("categoryList", categorySvc.findbyList(null));

		return resultMap;
	}
	
	/**
	 * 등록/수정
	 * */
	@RequestMapping(value={"/submit.do"}, method=RequestMethod.POST)
	public String submit(UrgencyNotice urgencyNotice, HttpServletRequest request, HttpSession session) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();
		
		if(urgencyNotice.getIdx() != null){
			urgencyNotice.setModifiedId(userId);
			urgencyNotice.setModifiedIp(ip);
			urgencyNoticeSvc.modify(urgencyNotice);
		}else{
			urgencyNotice.setCreatedId(userId);
			urgencyNotice.setCreatedIp(ip);
			urgencyNoticeSvc.add(urgencyNotice);
		}

		return "urgencyNotice/result";
	}
	
	/**
	 * 삭제
	 * */
	@RequestMapping(value={"/remove.do"}, method=RequestMethod.POST)
	public String remove(HttpServletRequest request, HttpSession session) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();

		int[] idx = ServletRequestUtils.getIntParameters(request, "idx");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		condition.put("modifiedId", userId);
		condition.put("modifiedIp", ip);
		condition.put("idx", idx);
		
		urgencyNoticeSvc.remove(condition);
		return "urgencyNotice/result";
	}	

}